* change directory
cd /N/project/iuni_doctorshopping/projects/covid_opioid/data/optum

* load data
use pain_treatment_transition, clear 

table state_t0 state_t1 if year == 2019 & period == 1
*-----------------------------------------------------------------
*         |                         state_t1                      
*         |          0         1           2         3       Total
*---------+-------------------------------------------------------
*0      |  2,341,542   208,033     372,480   34,505   2,956,560
*1      |    200,119   139,431      38,550   13,134     391,234
*2      |    337,493    30,108   1,472,145   35,817   1,875,563
*3      |     30,040    12,588      39,990   11,130      93,748
*Total  |  2,909,194   390,160   1,923,165   94,586   5,317,105

table state_t0 state_t1 if year == 2019 & period == 2
*-----------------------------------------------------------------
*         |                         state_t1                      
*         |          0         1           2         3       Total
*---------+-------------------------------------------------------
*state_t0 |                                                       
*  0      |  3,872,094   338,359     620,123    56,293   4,886,869
*  1      |    329,828   230,050      64,242    21,592     645,712
*  2      |    572,253    51,398   2,593,838    62,941   3,280,430
*  3      |     51,684    22,158      71,209    19,874     164,925
*  Total  |  4,825,859   641,965   3,349,412   160,700   8,977,936
*-----------------------------------------------------------------

table state_t0 state_t1 if year == 2020 & period == 1
*----------------------------------------------------------------
*         |                        state_t1                      
*         |          0         1           2        3       Total
*---------+------------------------------------------------------
*state_t0 |                                                      
*  0      |  2,317,925   204,509     351,187   33,759   2,907,380
*  1      |    189,820   136,285      34,939   11,216     372,260
*  2      |    347,189    30,962   1,437,478   35,832   1,851,461
*  3      |     30,654    13,553      39,257   11,145      94,609
*  Total  |  2,885,588   385,309   1,862,861   91,952   5,225,710
*----------------------------------------------------------------

table state_t0 state_t1 if year == 2020 & period == 2
*-----------------------------------------------------------------
*         |                         state_t1                      
*         |          0         1           2         3       Total
*---------+-------------------------------------------------------
*state_t0 |                                                       
*  0      |  3,050,979   288,662     384,836    40,956   3,765,433
*  1      |    290,827   249,022      44,992    16,981     601,822
*  2      |    362,482    36,098   1,439,989    39,128   1,877,697
*  3      |     36,452    17,252      45,194    14,506     113,404
*  Total  |  3,740,740   591,034   1,915,011   111,571   6,358,356
*-----------------------------------------------------------------

gen state_t1_0 = state_t1 == 0
gen state_t1_1 = state_t1 == 1
gen state_t1_2 = state_t1 == 2
gen state_t1_3 = state_t1 == 3

forvalues i = 0/3 {
	forvalues j = 0/3 {
	
		di " ... now running for t0 = `i' and t1 = `j' ..."	
		mean state_t1_`j', over(period), if year == 2019 & state_t0 == `i'
		matrix transition_p_`i'_`j'_2019 = r(table)
		mat2txt, matrix(transition_p_`i'_`j'_2019) title("transition_p_`i'_`j'_2019") saving("mean_transition_period.txt") append
		
		mean state_t1_`j', over(period), if year == 2020 & state_t0 == `i'
		matrix transition_p_`i'_`j'_2020 = r(table)
		mat2txt, matrix(transition_p_`i'_`j'_2020) title("transition_p_`i'_`j'_2020") saving("mean_transition_period.txt") append

		reg state_t1_`j' i.year, cluster(PATID), if state_t0 == `i' & period == 1 
		matrix transition_diff_`i'_`j'_period1 = r(table)
		mat2txt, matrix(transition_diff_`i'_`j'_period1) title("transition_diff_`i'_`j'_period1") saving("diff_transition_period.txt") append

		reg state_t1_`j' i.year, cluster(PATID), if state_t0 == `i' & period == 2 
		matrix transition_diff_`i'_`j'_period2 = r(table)
		mat2txt, matrix(transition_diff_`i'_`j'_period2) title("transition_diff_`i'_`j'_period2") saving("diff_transition_period.txt") append

	}
}



